package de.gameduell.framework.game.card.display {
	import flash.geom.Point;
	
	import de.gameduell.framework.game.card.Card;
	import de.gameduell.framework.game.card.CardStack;
	import de.gameduell.framework.geom.Geometry;	

	public class Fan implements IDisplayMode {

		private var _fanRadius:Number;
		private var _offset:Number;
		private var _center:Boolean;
		private var _cardLimit:int;
		
		/**  
		 * @param aRadius      = radius of the fan
		 * @param aOffset      = offset of the cards to each other
		 * @param centerActive = determines if the cards of the stack are centered or not
		 * @param aCardLimit   = sets a limit to the number of cards in the stack for wich the offset is changed
		**/
		public function Fan(aRadius:Number, aOffset:Number = 5, centerActive:Boolean = true, aCardLimit:int = 10){
			_fanRadius = aRadius;
			_offset	   = aOffset;
			_center	   = centerActive;
			_cardLimit = aCardLimit;	
		}
		
		/**  
		 * @param aLimit = sets a limit to the number of cards in the stack for wich the offset is changed
		**/
		public function set cardLimit(aLimit:int):void {
			_cardLimit = aLimit;
		}
		
		/**  
		 * @return Number = returns the radius of the fan
		**/
		public function get fanRadius():Number{
			return _fanRadius;
		}
		
		/**  
		 * @param newFanradius = sets the radius of the fan
		**/
		public function set fanRadius(newFanradius:Number):void{
			 _fanRadius = newFanradius;
		}
		
		/**  
		 * @return Number = offset of the cards to each other
		**/
		public function get offset():Number{
			return _offset;
		}
		
		/**  
		 * @param newOffset = offset of the cards to each other
		**/
		public function set offset(newOffset:Number):void{
			 _offset = newOffset;
		}
		
		/**  
		 * @param centered = determines if the cards of the cardstack are centered or not
		**/
		public function set center(centered:Boolean):void {
			_center = centered;
		}
		
		/**  places the cards of the stack on the desired spots
		 * @param stack = stack on wich the cards are placed
		**/
		public function updateDisplay(stack:CardStack):void {
			var cards:Array 	= stack.cards;
			var stackPos:Point 	= stack.position;
			stack.position 		= new Point(0,0);
			var position:Point 	= stack.position;
			var tooBig:Boolean = (stack.size > _cardLimit);
			var realOffset:Number = tooBig ? (_offset *  _cardLimit)/stack.size : _offset;
			
			for (var i:Number = 0;i < stack.size; i++ ) {
				var card:Card = Card(cards[i]);
				stack.setChildIndex(card.sprite, i);
				var arcBase:Number = _center ? (cards.length-1)* -realOffset/2 : -realOffset/2;
								
				var tempX:Number = position.x + _fanRadius * Math.sin(Geometry.angleToArc(arcBase + i*realOffset)); 
				var tempY:Number = position.y - _fanRadius * Math.cos(Geometry.angleToArc(arcBase + i*realOffset));
				
				card.rotation = arcBase + i *realOffset;
				card.scaleCard(stack.scaleFactor);
				
				var aPos:Point = new Point(tempX, tempY + _fanRadius);
				card.position    = aPos;
			}
			stack.position = stackPos;
		}
	}
}
